package com.kws.system.service.merchant.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.common.core.enums.ModelOpType;
import com.kws.common.core.sys.Ksys;
import com.kws.common.core.utils.KConstants;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.system.domain.TbMerchant;
import com.kws.system.domain.merchant.CreateMerchantSettlementRq;
import com.kws.system.domain.merchant.MerchantExamineRq;
import com.kws.system.domain.merchant.QueryMerchantSettlementRq;
import com.kws.system.domain.merchant.TbMerchantExamine;
import com.kws.system.domain.merchant.info.QueryMerchantExamineInfo;
import com.kws.system.domain.merchant.info.QueryMerchantSettlementInfo;
import com.kws.system.mapper.merchant.MerchantSettlementMapper;
import com.kws.system.service.merchant.MerchantExamineService;
import com.kws.system.service.merchant.MerchantSettlementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <b>MerchantSettlementServiceImpl</b>
 *
 * @description: MerchantSettlementServiceImpl <br>
 * @date: 2024/12/5 10:19 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class MerchantSettlementServiceImpl extends ServiceImpl<MerchantSettlementMapper,TbMerchant> implements MerchantSettlementService {
    private static final Logger logger = LoggerFactory.getLogger(MerchantSettlementServiceImpl.class);

    @Autowired
    private MerchantSettlementMapper merchantSettlementMapper;

    @Autowired
    private MerchantExamineService merchantExamineService;


    @Override
    public Page<QueryMerchantSettlementInfo> queryMerchantSettlementPage(QueryMerchantSettlementRq rq) {
        Page<QueryMerchantSettlementInfo> page = new Page<>(rq.getPageNum(), rq.getPageSize());
        Page<QueryMerchantSettlementInfo> pageInfo = merchantSettlementMapper.queryMerchantSettlementPage(page,rq);
        return pageInfo;
    }

    @Override
    public QueryMerchantExamineInfo queryMerchantSettlementById(Long id) {
        return merchantSettlementMapper.queryMerchantSettlementById(id);
    }

    @Override
    public boolean deleteMerchantSettlementById(Long id) {
        String userId = SecurityUtils.getStringUserId();
        return merchantSettlementMapper.deleteMerchantSettlementById(id,userId);
    }

    @Override
    public boolean firstInstance(MerchantExamineRq rq) {
        TbMerchantExamine tbMerchantExamine = new TbMerchantExamine();
        tbMerchantExamine.setMerchantId(rq.getMerchantId());
        tbMerchantExamine.setExamineUser(SecurityUtils.getStringUserId());
        tbMerchantExamine.setExamineStatus(rq.getExamineStatus());
        tbMerchantExamine.setExamineTime(new Date());
        tbMerchantExamine.setExamineHierarchy("0");
        tbMerchantExamine.setRemark(rq.getRemark());
        //判断商家审核状态修改商家状态
        if ("0".equals(rq.getExamineStatus())){
            merchantSettlementMapper.updateMerchantStatus(1,SecurityUtils.getStringUserId(),rq.getMerchantId());
        }else if ("1".equals(rq.getExamineStatus())){
            merchantSettlementMapper.updateMerchantStatus(3,SecurityUtils.getStringUserId(),rq.getMerchantId());
        }
        boolean save = merchantExamineService.save(tbMerchantExamine);
        return save;
    }

    @Override
    public boolean secondInstance(MerchantExamineRq rq) {
        TbMerchantExamine tbMerchantExamine = new TbMerchantExamine();
        tbMerchantExamine.setMerchantId(rq.getMerchantId());
        tbMerchantExamine.setExamineUser(SecurityUtils.getStringUserId());
        tbMerchantExamine.setExamineStatus(rq.getExamineStatus());
        tbMerchantExamine.setExamineTime(new Date());
        tbMerchantExamine.setExamineHierarchy("1");
        tbMerchantExamine.setRemark(rq.getRemark());
        //判断商家审核状态修改商家状态
        if ("0".equals(rq.getExamineStatus())){
            merchantSettlementMapper.updateMerchantStatus(1,SecurityUtils.getStringUserId(),rq.getMerchantId());
        }else if ("1".equals(rq.getExamineStatus())){
            merchantSettlementMapper.updateMerchantStatus(0,SecurityUtils.getStringUserId(),rq.getMerchantId());
        }
        boolean save = merchantExamineService.save(tbMerchantExamine);
        return save;
    }

    @Override
    public boolean createMerchantSettlement(CreateMerchantSettlementRq rq) {
        TbMerchant tbMerchant = new TbMerchant();
        BeanUtil.copyProperties(rq,tbMerchant);
        tbMerchant.setStatus(2);
        Ksys.setBasicModelData(tbMerchant,SecurityUtils.getStringUserId(), KConstants.DATA_ENABLE, ModelOpType.CREATE);
        int insert = baseMapper.insert(tbMerchant);
        if (1 == insert){
            return true;
        }
        return false;
    }
}
